<template>
	<view>
		<u--form :model='model' :rules='rules' ref="form1">
			<u-form-item label="手机号" prop="phone" borderBottom>
				<u--input v-model="model.phone" border="none"></u--input>
			</u-form-item>
			<u-form-item>
				<u-toast ref="uToast"></u-toast>
				<u-code :seconds="seconds" @end="end" @start="start" ref="uCode" 
				@change="codeChange"></u-code>
				<u-button @tap="getCode">{{tips}}</u-button>
			</u-form-item>
			<u-form-item label="验证码" prop="code" borderBottom>
				<u--input v-model="model.code" border="none"></u--input>
			</u-form-item>
			<u-form-item label="密码" prop="password" borderBottom>
				<u--input v-model="model.password" border="none"></u--input>
			</u-form-item>
		</u--form>
		<u-checkbox-group activeColor="#4293F5">
		    <u-checkbox  :checked="checked"  labelColor="#171717" labelSize="12" label="同意"  @change="getchecked"></u-checkbox>
		</u-checkbox-group>
		<u-button @click='submit'>提交</u-button>
	</view>
</template>

<script>
import { sendCode , forget } from '@/api/login.js'
export default{
	data () {
		return {
			checked:false,
			validate_code_id:'',
			tips: '',
			seconds:10,
			model:{
				phone:'',
				code:'',
				password:'',
				rePassword:''
			},
			rules:[]
		}
	},
	methods:{
		codeChange(text) {
			this.tips = text;
		},
		getCode() {
			if(this.$refs.uCode.canGetCode) {
				// 模拟向后端请求验证码
				uni.showLoading({
					title: '正在获取验证码'
				})
				setTimeout(() => {
					uni.hideLoading();
					// 这里此提示会被this.start()方法中的提示覆盖
					uni.$u.toast('验证码已发送');
					// 通知验证码组件内部开始倒计时
					this.$refs.uCode.start();
				}, 2000);
			} else {
				uni.$u.toast('倒计时结束后再发送');
			}
		},
		end() {
			uni.$u.toast('倒计时结束');
		},
		start() {
			uni.$u.toast('倒计时开始');
			sendCode({
				'_mall_id':1,
				'r':'api/user/phone-code',
				//'mobile':Encrypt(this.model.tel)
				'mobile':this.model.phone
			}).then(res=>{
				if( res.code != 0 ){
					//短信验证码发送不成功
					return;
				}
				this.validate_code_id = res.validate_code_id;
			})	
		},
		getchecked( val ){
			console.log( val )
		},
		//提交修改
		submit(){
			forget({
				mobile:this.model.phone,
				password:this.model.password,
				validate_code_id:this.validate_code_id,
				code:this.model.code
			}).then(res=>{
				if( res.code != 0 ) alert('不成功');
				uni.navigateTo({
					url:'/pages/login/login'
				})
			})
		}
	}
}
</script>

<style>
</style>